package com.nuoniu.sibanyun.service.msg;

import lombok.extern.log4j.Log4j2;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 消息推送
 * @auther 小啊刚
 * @date 2022-01-11
 */
@ServerEndpoint("/webSocket/{userId}")
@Component
@Log4j2
public class WebSocketService {

    //当前链接用户
    private static Map<String, Session> sessionPool = new HashMap<>();
    private static Map<String, String> userSessionPool = new HashMap<>();

    /**
     * 用户链接
     * @author 小啊刚
     * @date 2022-01-11
     */
    @OnOpen
    public void onOpen(Session session, @PathParam(value="userId")String userId) {
        sessionPool.put(userId, session);
        userSessionPool.put(session.getId(), userId);
        log.debug("建立链接" + userId + ",当前链接数量" + sessionPool.size() + ",当前用户数量:" + userSessionPool.size());
    }

    /**
     * 触发关闭
     * @author 小啊刚
     * @date 2022-01-11
     */
    @OnClose
    public void onClose(Session session) {
        String sessionId = session.getId();
        String userId = userSessionPool.get(sessionId);
        sessionPool.remove(userId);
        userSessionPool.remove(sessionId);
        log.debug("关闭链接,当前链接数量:" + sessionPool.size() + ",当前用户数量:" + userSessionPool.size());
    }

    /**
     * 接收客户端消息
     * @author 小啊刚
     * @date 2022-01-12
     */
    @OnMessage
    public void onMessage(String message) {
        log.debug("【webSocket消息】收到客户端消息:" + message);
    }

    /**
     * 广播消息
     * @author 小啊刚
     * @date 2022-01-12
     */
    public void sendAllMessage(Integer userId, String message) {
        log.debug("【webSocket消息】广播消息:" + message);
        sessionPool.forEach((id, session) -> {
            try {
                session.getBasicRemote().sendText(message);
            } catch (IOException e) {
                e.printStackTrace();
            }
        });
    }

    /**
     * 单点消息
     * @author 小啊刚
     * @date 2022-01-12
     */
    public void sendOneMessage(String userId, String message) {
        log.debug("【webSocket消息】单点消息:" + message);
        Session session = sessionPool.get(userId);
        if (session != null) {
            try {
                session.getAsyncRemote().sendText(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 单点 传递对象消息
     * @param userId
     * @param message
     */
    public void sendObjectMessage(String userId, Object message) {
        log.debug("【webSocket消息】单点对象消息:{}",message);
        Session session = sessionPool.get(userId);
        if (session != null) {
            try {
                session.getAsyncRemote().sendObject(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
